package com.tender.utils;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class CustomDateDeserializerUtil extends JsonDeserializer<Date> {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        String dateStr = jsonParser.getText();
        if ("null".equals(dateStr) || dateStr == null || dateStr.isEmpty()) {
            return null;  // 如果是 "null" 字符串或空字符串，返回 null
        }
        try {
            return dateFormat.parse(dateStr);
        } catch (Exception e) {
            log.error("*************************************");
            log.error("【json 反序列化时间串异常】时间字符串为：{}, 具体错误为：", dateStr, e);
            log.error("*************************************");
            return null;  // 解析失败时返回 null
        }
    }
}
